package com.zzzzzz.content.topic;

import java.util.List;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

public class TopicForm {
	@NotBlank
	private String title;
	@NotBlank
	private String text;
	@NotBlank
	private String tags;
	
	private CommonsMultipartFile files;
	private String srcUser;
	private String srcName;
	@NotBlank
	private String commentText;
	
	private String categoryId;
	
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public CommonsMultipartFile getFiles() {
		return files;
	}
	public void setFiles(CommonsMultipartFile files) {
		this.files = files;
	}
	public String getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	public String getCommentText() {
		return commentText;
	}
	public void setCommentText(String commentText) {
		this.commentText = commentText;
	}
	public String getSrcUser() {
		return srcUser;
	}
	public void setSrcUser(String srcUser) {
		this.srcUser = srcUser;
	}
	public String getSrcName() {
		return srcName;
	}
	public void setSrcName(String srcName) {
		this.srcName = srcName;
	}
	
	public void updateTopic(Topic topic){
		topic.setTitle(this.getTitle());
		topic.setText(this.getText());
		topic.setCategoryId(this.getCategoryId());
		topic.setCommentText(this.getCommentText());
		topic.setSrcName(this.getSrcName());
		topic.setSrcUser(this.getSrcUser());
		
		String tags = StringUtils.replace(this.getTags(), "，", ",");
		Iterable<String> tagIterable = Splitter.on(',').omitEmptyStrings().trimResults().split(tags);
		List<String> tagList = Lists.newArrayList(tagIterable);
		topic.setTags(tagList);
	}

	public void updateTopicForm(Topic topic){
		this.setTitle(topic.getTitle());
		this.setText(topic.getText());
		this.setCategoryId(topic.getCategoryId());
		this.setCommentText(topic.getCommentText());
		this.setSrcName(topic.getSrcName());
		this.setSrcUser(topic.getSrcUser());
		
		String tags = Joiner.on(", ").join(topic.getTags());
		this.setTags(tags);
	}
}
